package formServlets;
import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

import javax.servlet.http.HttpServlet;

//import guestbook.PMF;

public class  GetSchoolListServlet  extends HttpServlet  {

	/////////////////////////////////////////////////////////////////////
	// doGet forwards the request to doPost, the code does the same thing
	// whether the post method is GET or POST
	////////////////////////////////////////////////////////////////////
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		
		// Open access to the database
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		// Fetch the information from the form submitted by the page that called this servlet
		System.out.println("fetching school list");
		String country= request.getParameter("country");
		System.out.println(country);
		String state= request.getParameter("state");
		
		Query q = new Query("PlaceInfo");
		q.addFilter("Country", Query.FilterOperator.EQUAL,country);
		q.addFilter("State", Query.FilterOperator.EQUAL,state);
		PreparedQuery pq = datastore.prepare(q);
		System.out.println(FetchOptions.Builder.withPrefetchSize(1));
		int count=pq.countEntities(FetchOptions.Builder.withPrefetchSize(1));
		String placeid=new String();
		String schools=new String();
		System.out.println("number of entries in database with same placeID= "+ count);
		if(count >0)
		{
			for (Entity result : pq.asIterable())
			{
				placeid=(String) result.getProperty("placeId");
				System.out.println("placeId="+placeid);
			}
			Query q1 = new Query("SchoolInfo");
			q1.addFilter("placeId", Query.FilterOperator.EQUAL,placeid);
			pq = datastore.prepare(q1);
			for (Entity result : pq.asIterable())
			{
				schools=schools+","+(String) result.getProperty("schoolName");
				System.out.println((String) result.getProperty("schoolId"));
		    }
			
		}
		response.setContentType("text/html");
		response.getWriter().write(schools);
		
	}
 
}
